home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / Char Set & String Stuff / CharSet source code / not inlined.cpp < prev    next >
Text File  |  2000-11-07  |  13KB  |  338 lines

  1.  
  2.  
  3. /*
  4.         MemoryBlock.InByte, InRevByte, InSegBlock, InBlock, InCharSet, OutCharSet, SetSize;
  5.         Global StringToPointer, MidAscB, ByteOfString, InCharSet, OutCharSet, InByte, EmptyString, FastInStrB
  6.         functions by "Theo", <http://www.thsmith.dircon.co.uk/> <delete@softhome.net>
  7.  
  8.  Oct 28, 2000, v1.0
  9.  Aug 11, 2000, v1.201
  10.  
  11. I did some tests on speed, and it seems that adding a long that happens to be 1 or -1, to another variable
  12. is just as fast as doing ++ or --
  13.  
  14. Also, it seems that without inlining, that calling one function from inside another is just as fast
  15. as putting one function's code inside the other.....
  16.  
  17. The slowest part often seems to be RB's overhead in calling the plugin, passing values and returning them.
  18.  
  19.  */
  20.  
  21.  
  22.  
  23. #ifndef WIN32
  24.     #include <MacMemory.h>
  25. #endif
  26.  
  27. #include "rb_plugin.h"
  28.  
  29. typedef long* byreflong;
  30. typedef const char cnst;
  31. typedef unsigned const char ucnst;
  32. typedef unsigned char uchar;
  33.  
  34. long kFirst =  0;
  35. long kBack  = -1;
  36. long kFore  =  1;
  37. long kEnd   = -1;
  38.  
  39. extern REALclassDefinition memBlkClass;
  40.  
  41.  
  42.  
  43.  
  44.  
  45.         /* InLines */
  46.  
  47. static inline uchar* MBPtr (REALobject self) {
  48.  return *(uchar**)((uchar*)self + 0x1C); }
  49.  
  50. static inline long MBSize (REALobject self) {
  51.  return *(long*)((long)self + 0x18); }
  52.  
  53. static inline void setMBSize (REALobject self, long NewSize) {
  54.  *(long*)((long)self + 0x18) = NewSize; }
  55.  
  56. static inline uchar* SearchContainerUptil(uchar* CharPos, long Finish, long ContainerLength) { //Finish 0 based, CLength 1 based, result 1 based
  57.  if (Finish < 0 || Finish >= ContainerLength)
  58.   return (uchar*)(CharPos + ContainerLength);
  59.  else
  60.   return (uchar*)(CharPos + Finish + 1); }
  61.  
  62. static inline void CharPosAndContainerEndHandler(byreflong CharPos, byreflong ContainerEnd, byreflong dirlong) {
  63.    if (*dirlong == 1) {
  64.     if (*CharPos > *ContainerEnd)
  65.      *CharPos = *ContainerEnd;
  66.   } else {
  67.     long TempCharPos = *CharPos - 1;
  68.     *CharPos = *ContainerEnd - 1;
  69.     *ContainerEnd = TempCharPos;
  70.     if (*ContainerEnd > *CharPos)
  71.      *CharPos = *ContainerEnd;
  72.   }
  73. }
  74.  
  75.  
  76.  
  77.  
  78.          /*       Main subs       */
  79.  
  80. static long    outcmainsub(uchar *CharPos, long BeginPos, long EndPos, uchar *CharSetOffset, long LenBString, long LenBCharSet, long dirlong) {
  81.  register ucnst        *CharSetPos;
  82.  ucnst                *FirstCharPos;
  83.  uchar                *CharSetEnd;
  84.  uchar                *SearchEnd;
  85.  
  86.  if (LenBString && LenBCharSet) {
  87.   FirstCharPos = CharPos;
  88.   SearchEnd = SearchContainerUptil(CharPos, EndPos, LenBString);
  89.   CharSetEnd = (uchar*)((long)CharSetOffset + LenBCharSet);
  90.   if (BeginPos > 0) CharPos += BeginPos;
  91.   CharPosAndContainerEndHandler((byreflong)&CharPos, (byreflong)&SearchEnd, (byreflong)&dirlong);
  92.   for (CharPos; CharPos != SearchEnd; CharPos += dirlong) {
  93.    for (CharSetPos = (uchar*)CharSetOffset; CharSetPos != CharSetEnd; CharSetPos++) {
  94.     if (*CharSetPos == *CharPos) goto EndLoop;
  95.    }
  96.    return (long)(CharPos - FirstCharPos);
  97.    EndLoop: ;
  98.   }
  99.  }
  100.  return -1;
  101. }
  102.  
  103.  
  104. static long    incmainsub (uchar *CharPos, long BeginPos, long EndPos, uchar *CharSetOffset, long LenBString, long LenBCharSet, long *csresult, long dirlong) {
  105.  ucnst                *FirstCharPos;
  106.  register uchar        *CharSetPos;
  107.  uchar                *CharSetEnd;
  108.  uchar                *SearchEnd;
  109.  
  110.  if (LenBString && LenBCharSet) {
  111.   FirstCharPos = CharPos ;
  112.   SearchEnd = SearchContainerUptil(CharPos, EndPos, LenBString) ;
  113.   CharSetEnd = (uchar*)((long)CharSetOffset + LenBCharSet) ;
  114.   if (BeginPos > 0) CharPos += BeginPos;
  115.   CharPosAndContainerEndHandler((byreflong)&CharPos, (byreflong)&SearchEnd, (byreflong)&dirlong);
  116.   for (CharPos; CharPos != SearchEnd; CharPos+= dirlong) {
  117.    for (CharSetPos = (uchar*)CharSetOffset; CharSetPos != CharSetEnd; CharSetPos++) {
  118.     if (*CharSetPos == *CharPos) {
  119.      *csresult = CharSetPos - CharSetOffset;
  120.      return (long)(CharPos - FirstCharPos) ;
  121.     }
  122.    }
  123.   }
  124.  }
  125.  return -1;
  126. }
  127.  
  128.  
  129.  
  130. static long inblockmainsub(uchar *CharPos, long BeginPos, long EndPos, uchar *SearchStart, long BeginSear, long EndSear, long ContainerLength, long SearchLength, long dirlong) {
  131.  register uchar        *SearchPos;
  132.  register uchar        FirstChar;
  133.  register uchar        *TempCharPos;
  134.  uchar                *FirstCharPos;
  135.  uchar                *SearchEnd;
  136.  uchar                *ContainerEnd;
  137.   
  138.  if ( ContainerLength ) {
  139.   FirstCharPos = CharPos;
  140.   SearchEnd = SearchContainerUptil(SearchStart, EndSear, SearchLength);
  141.   if ( SearchEnd >= SearchStart ) {
  142.    if (BeginSear > 0) SearchStart += BeginSear;
  143.    SearchLength = SearchEnd - SearchStart;
  144.    ContainerLength += SearchLength;
  145.    ContainerEnd = SearchContainerUptil(CharPos, EndPos, ContainerLength);
  146.    if (BeginPos > 0) CharPos += BeginPos;
  147.    CharPosAndContainerEndHandler((byreflong)&CharPos, (byreflong)&ContainerEnd, (byreflong)&dirlong);
  148.    FirstChar = *SearchStart;
  149.    SearchStart++;
  150.    for (CharPos; CharPos != ContainerEnd; CharPos += dirlong) {
  151.     if (FirstChar == *CharPos) {
  152.      TempCharPos = CharPos + 1;
  153.      for (SearchPos = SearchStart; SearchPos != SearchEnd;) {
  154.       if (*SearchPos++ != *TempCharPos++) goto DidntMatch;
  155.      }
  156.      return (long)CharPos - long(FirstCharPos);
  157.      DidntMatch: ;
  158.     }
  159.    }
  160.   }
  161.  }
  162.  return -1;
  163. }
  164.  
  165.  
  166.  
  167. static long inbytemainsub(uchar *CharPos, long ContainerLength, long BeginPos, long EndPos, long SearchByteParam, long dirlong) {
  168.  register uchar        SearchByte;
  169.  uchar                *SearchEnd;
  170.  uchar                *FirstCharPos;
  171.  
  172.  if (ContainerLength) {
  173.   
  174.   FirstCharPos = CharPos;
  175.   SearchEnd = SearchContainerUptil(CharPos, EndPos, ContainerLength);
  176.   if (BeginPos > 0) CharPos += BeginPos;
  177.   CharPosAndContainerEndHandler((byreflong)&CharPos, (byreflong)&SearchEnd, (byreflong)&dirlong);
  178.   SearchByte = (uchar)SearchByteParam;
  179.   for (CharPos; CharPos != SearchEnd; CharPos += dirlong) {
  180.    if (SearchByte == *CharPos) {return (long)CharPos - long(FirstCharPos);}
  181.   }
  182.  }
  183.  return -1;
  184. }
  185.  
  186.  
  187. static long InRange(REALobject instance, long BeginPos, long EndPos, long RangeStart, long RangeEnd) {
  188.   register uchar    *CharPos = MBPtr(instance);
  189.   uchar                *StartPos;
  190.   long                ContainerLength = MBSize(instance);
  191.   register uchar    *ContainerEnd;
  192.   long                templong;
  193.   StartPos = CharPos;
  194.   
  195.   if ( ContainerLength ) {
  196.        ContainerEnd = SearchContainerUptil(CharPos, EndPos, ContainerLength);
  197.        if (BeginPos > 0) CharPos += BeginPos;
  198.     for (CharPos; CharPos < ContainerEnd; CharPos++) {
  199.         templong = long(*CharPos);
  200.         if ( templong < RangeStart || templong > RangeEnd) return (CharPos - StartPos);
  201.     }
  202.  }
  203.  return -1;
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210. /* String functions. */
  211.  
  212. static long    OutCSStr(REALstring MyString, long BeginPos, long EndPos, REALstring CharSet) {
  213.  return 1 + outcmainsub((uchar*)MyString->CString(), BeginPos-1, EndPos-1, (uchar*)CharSet->CString(), MyString->Length(), CharSet->Length(), kFore); }
  214.  
  215. static long    OutRevCSStr(REALstring MyString, long BeginPos, long EndPos, REALstring CharSet) {
  216.  return 1 + outcmainsub((uchar*)MyString->CString(), BeginPos-1, EndPos-1, (uchar*)CharSet->CString(), MyString->Length(), CharSet->Length(), kBack); }
  217.  
  218. static long    InCSStr(REALstring MyString, long BeginPos, long EndPos, REALstring CharSet) {
  219.  long dummy;
  220.  return 1 + incmainsub((uchar*)MyString->CString(), BeginPos-1, EndPos-1, (uchar*)CharSet->CString(), MyString->Length(), CharSet->Length(), &dummy, kFore); }
  221.  
  222. static long FastInStrB(long BeginPos, long EndPos, REALstring MyString, REALstring pattern) {
  223.  return 1 + inblockmainsub( (uchar*)MyString->CString(), BeginPos-1, EndPos-1, (uchar*)pattern->CString(), kFirst, kEnd, MyString->Length(), pattern->Length(), kFore ); }
  224.  
  225. static long InByteStr(long BeginPos, long EndPos, REALstring MyString, long pByte) {
  226.  return 1 + inbytemainsub((uchar*)MyString->CString(), MyString->Length(), BeginPos-1, EndPos-1, pByte, kFore); }
  227.  
  228. static long StrToPointer(REALstring mystring) {
  229.  return (long)(mystring->CString()); }
  230.  
  231. static long MidAscB(REALstring mystring, long offset) {
  232.  return (long)(*(uchar*)((long)(mystring->CString()) + offset - 1)); }
  233.  
  234. static long ByteOfString(REALstring mystring, long offset) {
  235.  return (long)(*(uchar*)((long)(mystring->CString()) + offset)); }
  236.  
  237. static REALstring EmptyString(long TheLength, long pChar) {
  238.  REALstring resultstring = REALBuildString (0, (TheLength > -1 ? TheLength : 0));
  239.  if (TheLength > 0 && pChar > -1 && pChar < 256) {
  240.       uchar *charpos = (uchar*)resultstring->CString(), *resultend = charpos + TheLength;
  241.       uchar TheChar = (uchar)pChar;
  242.       for (charpos; charpos < resultend; charpos++) {
  243.           *charpos = TheChar;
  244.       }
  245.   }
  246.  return resultstring;
  247. }
  248.  
  249.  
  250.  
  251.            /* Memoryblock methods */
  252.  
  253. static long InBlock(REALobject self, long BeginPos, long EndPos, REALobject SearchMB) {
  254.  return inblockmainsub( MBPtr(self), BeginPos, EndPos, MBPtr(SearchMB), kFirst, kEnd, MBSize(self), MBSize(SearchMB), kFore ); }
  255.  
  256. static long InRevBlock(REALobject self, long BeginPos, long EndPos, REALobject SearchMB) {
  257.  return inblockmainsub( MBPtr(self), BeginPos, EndPos, MBPtr(SearchMB), kFirst, kEnd, MBSize(self), MBSize(SearchMB), kBack ); }
  258.  
  259. static long ExpertInBlock(REALobject self, long BeginPos, long EndPos, REALobject SearchMB, long BeginSear, long EndSear, Boolean dir) {
  260.  return inblockmainsub( MBPtr(self), BeginPos, EndPos, MBPtr(SearchMB), BeginSear, EndSear, MBSize(self), MBSize(SearchMB), (dir ? kFore : kBack)); }
  261.  
  262. static long InByteMB(REALobject self, long BeginPos, long EndPos, long SearchByteParam) {
  263.  return inbytemainsub( MBPtr(self), MBSize(self), BeginPos, EndPos, SearchByteParam, kFore); }
  264.  
  265. static long InRevByteMB(REALobject self, long BeginPos, long EndPos, long SearchByteParam) {
  266.  return inbytemainsub( MBPtr(self), MBSize(self), BeginPos, EndPos, SearchByteParam, kBack); }
  267.  
  268. static long OutCSMB(REALobject self, long BeginPos, long EndPos, REALobject CharSetMB) {
  269.  return outcmainsub(MBPtr(self), BeginPos, EndPos, MBPtr(CharSetMB), MBSize(self), MBSize(CharSetMB), kFore); }
  270.  
  271. static long    InCSMB(REALobject self, long BeginPos, long EndPos, REALobject CharSetMB, long *charsetresult) {
  272.  return incmainsub(MBPtr(self), BeginPos, EndPos, MBPtr(CharSetMB), MBSize(self), MBSize(CharSetMB), charsetresult, kFore); }
  273.  
  274. static void SetMBSize(REALobject self, long NewSize) {
  275.  setMBSize(self, NewSize); }
  276.  
  277. //static void SetMBColor(REALobject self, long offset, long TheColor/*A cheat, but it works*/) {
  278. // *(long*)(MBPtr(self) + offset) = TheColor; }
  279.  
  280. //static long GetMBColor(REALobject self, long offset) {
  281. //return *(long*)(MBPtr(self) + offset); }
  282.  
  283.  
  284.             /* Plugin glue code */
  285.  
  286. static REALmethodDefinition memBlkClassMethods[] = {
  287.     { (REALproc) OutCSMB,              REALnoImplementation, "OutCharSet(Start as Integer, Finish as Integer, CharSet as MemoryBlock) as Integer"},
  288.     { (REALproc) InCSMB,              REALnoImplementation, "InCharSet(Start as Integer, Finish as Integer, CharSet as MemoryBlock, byref CharSetPos as Integer) as Integer"},
  289.     { (REALproc) InRange,              REALnoImplementation, "InRange(Start as Integer, Finish as Integer, RangeStart as integer, RangeEnd as integer) as Integer"},
  290.     { (REALproc) InBlock,            REALnoImplementation, "InBlock(Start as Integer, Finish as Integer, Search as MemoryBlock) as Integer"},
  291.     { (REALproc) InRevBlock,        REALnoImplementation, "InRevBlock(Start as Integer, Finish as Integer, Search as MemoryBlock) as Integer"},
  292.     { (REALproc) ExpertInBlock,        REALnoImplementation, "InSegBlock(Start as Integer, Finish as Integer, Search as MemoryBlock, SrStart as integer, SrFinish as Integer, Forward as Boolean) as Integer"},
  293.     { (REALproc) InByteMB,              REALnoImplementation, "InByte(Start as Integer, Finish as Integer, Byte as Integer) as Integer"},
  294.     { (REALproc) InRevByteMB,        REALnoImplementation, "InRevByte(Start as Integer, Finish as Integer, Byte as Integer) as Integer"},
  295.     { (REALproc) SetMBSize,            REALnoImplementation, "SetSize(Size as Integer)" },
  296. //    { (REALproc) GetMBColor,        (REALproc)SetMBColor, "Color(Offset as Integer) as Color"},
  297. };
  298.  
  299. static REALmethodDefinition pluginMethodArray[] = {
  300.     { (REALproc) EmptyString,        REALnoImplementation, "EmptyString(Length as Integer, CharVal as integer) as String" },
  301.     { (REALproc) FastInStrB,        REALnoImplementation, "FastInStrB(Start as Integer, Finish as integer, Container as String, Find as string) as Integer" },
  302.     { (REALproc) InByteStr,            REALnoImplementation, "InByte(Start as Integer, Finish as integer, Container as String, Byte as Integer) as Integer" },
  303.     { (REALproc) OutCSStr,            REALnoImplementation, "OutCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer" },
  304.     { (REALproc) OutRevCSStr,        REALnoImplementation, "OutRevCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer" },
  305.     { (REALproc) InCSStr,            REALnoImplementation, "InCharSet(Container as String, Start as Integer, Finish as Integer, CharSet as String) as Integer" },
  306.     { (REALproc) MidAscB,            REALnoImplementation, "MidAscB(Container as String, Offset as Integer) as Integer" },
  307.     { (REALproc) ByteOfString,        REALnoImplementation, "ByteOfString(Container as String, Offset as Integer) as Integer" },
  308.     { (REALproc) StrToPointer,        REALnoImplementation, "StringToPointer(TheString as String) as Integer" },
  309. };
  310.  
  311.  
  312.  
  313.             /* Wierd Stuff I still don't get yet */
  314.  
  315. static REALclassDefinition memBlkClass = {
  316.     kCurrentREALControlVersion,
  317.     "MemoryBlock",
  318.     nil,
  319.     0,
  320.     0,
  321.     (REALproc) nil,
  322.     (REALproc) nil,
  323.     nil,
  324.     nil,
  325.     memBlkClassMethods,
  326.     sizeof(memBlkClassMethods) / sizeof(REALmethodDefinition),
  327.     nil,
  328.     0
  329. };
  330.  
  331. void PluginEntry (void)
  332. {
  333.     for (int i = 0; i < sizeof(pluginMethodArray) / sizeof(REALmethodDefinition); ++i) {
  334.         REALRegisterMethod (&pluginMethodArray[i]);
  335.     }    
  336.     REALRegisterClassExtension (&memBlkClass);
  337. }
  338.